using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace FeatureWally.Client.UI.Forms {
    public partial class WindowOptions : Form {
        public WindowOptions() {
            InitializeComponent();

            LoadSettings();
        }

        private void LoadSettings() {
            tbStsAdm.Text = FeatureWally.Client.Properties.Settings.Default.STSADM;
            tbExportBatFileLocation.Text = FeatureWally.Client.Properties.Settings.Default.StsAdmExportLocation;
        }

        private bool ValidateChangedSettings() {
            return true;
        }

        private void SaveSettings() {
            FeatureWally.Client.Properties.Settings.Default.STSADM = tbStsAdm.Text;
            FeatureWally.Client.Properties.Settings.Default.StsAdmExportLocation = tbExportBatFileLocation.Text;
            Properties.Settings.Default.Save();
            Properties.Settings.Default.Reload();
        }

        private void btnOK_Click(object sender, EventArgs e) {
            if (this.ValidateChildren()) {
                SaveSettings();
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnStsAdmSelect_Click(object sender, EventArgs e) {
            fileDialogStsAdm.InitialDirectory = tbStsAdm.Text;
            DialogResult result = fileDialogStsAdm.ShowDialog(this);
            if (result == DialogResult.OK) {
                tbStsAdm.Text = fileDialogStsAdm.FileName;
            }
        }

        private void tbStsAdm_TextChanged(object sender, EventArgs e) {
            
        }

        private void tbStsAdm_Validating(object sender, CancelEventArgs e) {
            try {
                if (tbStsAdm.Text.Length > 0 && !System.IO.File.Exists(tbStsAdm.Text))
                    e.Cancel = true;
            }
            catch { e.Cancel = true; }
            if (e.Cancel) {
                MessageBox.Show("Please enter a valid file path to where STSADM.exe is located");
            }
        }

        private void btnBatFileBrowse_Click(object sender, EventArgs e) {
            folderDialogBatFile.SelectedPath = tbExportBatFileLocation.Text;
            DialogResult result = folderDialogBatFile.ShowDialog(this);
            if (result == DialogResult.OK) {
                tbExportBatFileLocation.Text = folderDialogBatFile.SelectedPath;
            }
        }

        private void tbExportBatFileLocation_Validating(object sender, CancelEventArgs e) {
            try {
                if (tbExportBatFileLocation.Text.Length > 0 && !System.IO.Directory.Exists(tbExportBatFileLocation.Text))
                    e.Cancel = true;
            }
            catch { e.Cancel = true; }
            if (e.Cancel) {
                MessageBox.Show("Please enter a valid folder path, where exported bat files default should be placed");
            }
        }

        private void tbStsAdm_Enter(object sender, EventArgs e) {
            
        }
    }
}